<?php
namespace App\Service\Admin;

use Illuminate\Support\Facades\DB;

class ItemService extends BaseService
{
    //项目

    //列表
    public function getIndex()
    {
        $res = $this->dbConn($this->dbItem)->where('del', 0)->get();
        if (!count($res)) {
            return $this->toback(0, '没有项目', []);
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'cn' => $v['cn'],
                'cover' => $v['cover'] ? env('DOMAIN').'/'.$v['cover'] : '',
            ];
        }
        return $this->toback(0, '', $arys);
    }

    //详情
    public function getOne($itemId)
    {
        $res = (array) $this->dbConn($this->dbItem)->where('id', $itemId)->first();
        if (empty($res)) {
            return $this->toback(0, '没有项目', []);
        }
        $arys = [
            'id' => $res['id'],
            'cn' => $res['cn'],
            'en' => $res['en'],
            'cover' => $res['cover'] ? env('DOMAIN').'/'.$res['cover'] : '',
            'item_admin' => env('DOMAIN') . '/ai_item/admin/fxotary',
            'item_front' => env('DOMAIN') . '/ai_item/front/fxotary',
        ];
        //文件树
        $arys['treeList'] = [
            'front_view' => $this->getTree($itemId, 2),
            'front_api' => [],
            'admin_view' => $this->getTree($itemId, 1),
            'admin_api' => [],
        ];
        return $this->toback(0, '', $arys);
    }

    /**
     * 项目文件树
     * @param int $item_id 项目ID
     * @param int $client_type 1管理后台页面，2管理后台接口，3用户端页面，4用户端接口
     */
    public function getTree($item_id, $client_type, $pid = 0)
    {
        $treeArys = [];
        $res = $this->dbConn($this->dbMkPath)
            ->where('item_id', $item_id)
            ->where('client_type', $client_type)
            ->where('pid', $pid)
            ->get();
        if (!count($res)) {
            return $treeArys;
        }
        foreach ($res as $v) {
            $v = (array) $v;
            $treeArys[] = [
                'fid' => $v['id'],
                'fname' => $v['title'],
                'fpath' => is_file($v['url']) ? $v['url'] : '',
                'childList' => $this->getTree($item_id, $client_type, $v['id']),
            ];
        }
        return $treeArys;
    }

    /**
     * 保存文件
     * @param int $uid
     * @param array $fs【$fname文件原名, $fpath文件地址, $forigin文件项目名】
     * @param array $pas【$type，$front】
     */
    public function addFile($uid, $fs, $pas)
    {
        $rar = $this->dbConn($this->dbMkRar)->insertGetId([
            'uid' => $uid, 'fname' => $fs['fname'],
            'fpath' => $fs['fpath'], 'fitem' => $fs['forigin'],
            'addtime' => time(),
        ]);
        if (!$rar) {
            return $this->toback(-1, '删除失败');
        }
        $item = $this->dbConn($this->dbItem)->insertGetId([
            'uid' => $uid, 'cn' => $fs['fname'], 'en' => $fs['forigin'],
            'type' => $pas['type'], 'stack_front' => $pas['front'],
            'stack_back' => $pas['front'], 'addtime' => time(),
        ]);
        if (!$item) {
            return $this->toback(-1, '项目失败');
        }
        return $this->toback(0, '', $item);
    }

    /**
     * 保存项目文件地址
     * @param string $itemDir 项目地址
     * @param int $item_id 项目ID
     * @param int $uid
     * @param int $pid 父级ID
     */
    public function addPath($itemDir, $item_id, $uid, $pid = 0)
    {
        $trees = scandir($itemDir);
        if (count($trees) <= 2) return false;
        $notIns = [
            '.', '..', 'css', 'fonts',
        ];
        foreach ($trees as $v) {
            if (!in_array($v, $notIns)) {
                $fDir = $itemDir.'/'.$v;
                $lastid = $this->dbConn($this->dbMkPath)->insertGetId([
                    'uid' => $uid, 'item_id' => $item_id, 'client_type' => 2,
                    'title' => $v, 'url' => $fDir,
                    'pid' => $pid, 'addtime' => toDate(),
                ]);
                $childList = [];
                if (is_dir($fDir)) {
                    $childList = $this->addPath($fDir, $item_id, $uid, $lastid);
                }
                $treeArys['front_view'][] = [
                    'fid' => $lastid,
                    'fname' => $v,
                    'fpath' => is_file($fDir) ? $fDir : '',
                    'childList' => $childList,
                ];
            }
        }
        return $treeArys;
    }

    //获取文件地址
    public function getFileOne($fid)
    {
        return (array) $this->dbConn($this->dbMkPath)->where('id', $fid)->first();
    }

    //添加项目文件
    public function addNewPath($pas)
    {
        $pas['addtime'] = toDate();
        return $this->dbConn($this->dbMkPath)->insertGetId($pas);
    }
}